//接口请求设置
package com.cuijian.demo.config;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;

import javax.servlet.http.HttpServletResponse;

public class CorsInterceptor implements Interceptor {

    private String allowOrigin = "*";
    private String allowMethods = "GET, POST, PUT, DELETE, OPTIONS";
    private String allowHeaders = "Content-Type, Authorization, X-Requested-With";
    private String allowCredentials = "true";
    private String maxAge = "3600";

    public CorsInterceptor() {}

    public CorsInterceptor(String allowOrigin) {
        this.allowOrigin = allowOrigin;
    }

    public CorsInterceptor(String allowOrigin, String allowMethods, String allowHeaders) {
        this.allowOrigin = allowOrigin;
        this.allowMethods = allowMethods;
        this.allowHeaders = allowHeaders;
    }

    @Override
    public void intercept(Invocation inv) {
        Controller controller = inv.getController();
        HttpServletResponse response = controller.getResponse();

        // 设置CORS头
        response.setHeader("Access-Control-Allow-Origin", allowOrigin);
        response.setHeader("Access-Control-Allow-Methods", allowMethods);
        response.setHeader("Access-Control-Allow-Headers", allowHeaders);
        response.setHeader("Access-Control-Allow-Credentials", allowCredentials);
        response.setHeader("Access-Control-Max-Age", maxAge);

        // 强制设置Content-Type为JSON
        response.setContentType("application/json; charset=utf-8");

        // 处理OPTIONS预检请求
        if ("OPTIONS".equalsIgnoreCase(controller.getRequest().getMethod())) {
            controller.renderNull();
            return;
        }

        inv.invoke();
    }

    // setter方法
    public void setAllowOrigin(String allowOrigin) { this.allowOrigin = allowOrigin; }
    public void setAllowMethods(String allowMethods) { this.allowMethods = allowMethods; }
    public void setAllowHeaders(String allowHeaders) { this.allowHeaders = allowHeaders; }
    public void setAllowCredentials(String allowCredentials) { this.allowCredentials = allowCredentials; }
    public void setMaxAge(String maxAge) { this.maxAge = maxAge; }
}
//public class CorsInterceptor implements Interceptor {
//    @Override
//    public void intercept(Invocation inv) {
//        HttpServletResponse response = inv.getController().getResponse();
//        // 设置CORS头
//        response.setHeader("Access-Control-Allow-Origin", "*");
//        response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
//        response.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization, X-Requested-With");
//        response.setHeader("Access-Control-Allow-Credentials", "true");
//        response.setHeader("Access-Control-Max-Age", "3600");
//        response.setContentType("application/json; charset=utf-8");
//        // 处理预检请求
//        if ("OPTIONS".equals(inv.getController().getRequest().getMethod())) {
//            inv.getController().renderNull();
//            return;
//        }
//
//        inv.invoke();
//    }
//}
